package com.ssy.lingxi.message.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.MemberFeignMsgByRoleIdVO;
import com.ssy.lingxi.message.api.vo.request.SendPlatformMessageRequest;
import com.ssy.lingxi.message.constant.MessageConstants;
import com.ssy.lingxi.message.entity.MainMessage;
import com.ssy.lingxi.message.entity.MemberMessage;
import com.ssy.lingxi.message.enums.CommonBooleanEnum;
import com.ssy.lingxi.message.enums.MainMessageTargetTypeEnum;
import com.ssy.lingxi.message.enums.MessageTypeEnum;
import com.ssy.lingxi.message.repository.MainMessageRepository;
import com.ssy.lingxi.message.repository.MemberMessageRepository;
import com.ssy.lingxi.message.service.IMainMessageService;
import com.ssy.lingxi.message.websocket.util.JsonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 主消息服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2020/10/27
 */
@Service
public class MainMessageService extends BaseMemberMessageService implements IMainMessageService {

    @Resource
    private MessageEventService messageEventService;
    @Resource
    private MainMessageRepository mainMessageRepository;
    @Resource
    private MemberFeignService memberFeignService;
    @Resource
    private MemberMessageRepository memberMessageRepository;

    public Wrapper<Void> sendPlatformMessage(SendPlatformMessageRequest request, UserLoginCacheDTO loginUser) {

        // 保存消息
        long currentTimeMillis = System.currentTimeMillis();
        MainMessage mainMessage = new MainMessage();
        mainMessage.setType(MessageTypeEnum.PLATFORM.getCode());
        mainMessage.setTarget(JsonUtil.toJsonStr(request.getRoleIds()));
        mainMessage.setTargetType(MainMessageTargetTypeEnum.ROLE_IDS.getCode());
        mainMessage.setAction("");
        mainMessage.setTitle(request.getTitle());
        mainMessage.setContent(request.getContent());
        mainMessage.setUrl(request.getUrl());
        mainMessage.setExpire(currentTimeMillis + MessageConstants.expire);
        mainMessage.setCreateTime(currentTimeMillis);
        mainMessageRepository.saveAndFlush(mainMessage);

        // 平台后台消息
        MemberMessage memberMessage = new MemberMessage();
        memberMessage.setMainMessageId(mainMessage.getId());
        memberMessage.setSendMemberId(MSG_PF_SEND_MEMBER_ID);
        memberMessage.setSendRoleId(MSG_PF_SEND_ROLE_ID);
        memberMessage.setRecMemberId(MSG_PF_REC_MEMBER_ID);
        memberMessage.setRecRoleId(MSG_PF_REC_ROLE_ID);
        memberMessage.setStatus(CommonBooleanEnum.NO.getCode());
        memberMessage.setDeleted(CommonBooleanEnum.NO.getCode());
        memberMessage.setType(MessageTypeEnum.PLATFORM.getCode());
        memberMessage.setTitle(request.getTitle());
        memberMessage.setContent(request.getContent());
        memberMessage.setUrl(request.getUrl());
        memberMessage.setSendTime(mainMessage.getCreateTime());
        memberMessage.setCreateTime(System.currentTimeMillis());
        memberMessageRepository.saveAndFlush(memberMessage);

        messageEventService.onPlatformMessage();

        List<MemberFeignMsgByRoleIdVO> memberUsers = memberFeignService.getUserByRoleIds(request.getRoleIds());
        for (MemberFeignMsgByRoleIdVO memberUser : memberUsers) {
            messageEventService.onMessage(memberUser.getMemberId(), memberUser.getRoleId(), memberUser.getUserId());
        }

        return Wrapper.success();
    }
}
